define( function () {
  var $username = $( '.user-name' );
  var $code = $( '.code' );
  var $answer = $( '.answer' );
  var $form = $( '.form' );
  var $validateCode = $( '.change-code' );
  var urls = {
    username: 'register?action=validateUsernameOrEmailExists&username=',
    code: 'register?action=validateCode&validateCode=',
    answer: '../account/fpwd?action=checkQuestionAnswerCorrect&username='+$("#username").val()+'&questionIndex='+$("#questionIndex").val()+'&answer=',
  	getnextquestion: '../account/fpwd?action=getNextQuestion&username='+$("#username").val()+'&questionIndex='
  };

  function init () {
    $username && $username.bind( 'blur', function () {
      checker( this, 'username' );
    } );

    $code && $code.bind( 'blur', function () {
      checker( this, 'code' );
    } );

    $answer && $answer.bind( 'blur', function () {
      checker( this, 'answer' );
    } );

    $form.bind( 'submit', function () {
      return submit( this );
    } );
    
    $validateCode.bind( 'click', function () {
        $("#yanzheng").attr( "src", "../ajax/verifyhandler.ashx?rand=" + new Date()/100 );
        return false;
      } );
    
    $( '#getNextQuestion' ).bind( 'click', function () {
      	getNextQuestion('getnextquestion' );
      } );
  }

  // 妫�煡閭楠岃瘉鐮侊紝鎵嬫満楠岃瘉鐮侊紝瀵嗕繚绛旀
  function checker ( _this, st ) {
    var url = urls[ st ];

    if ( _this.value != '' )
      {
        $.get( url + _this.value, function( result ) {
          if( result == "true" ) {
        	hideErrorLayer( _this);
            _this.isSubmit = true;
          }
          else
          {
            showErrorLayer( _this );
            _this.isSubmit = false;
          }
        } );
      }
      else
      {
          showErrorLayer( _this );
          _this.isSubmit = false;
      }
  }
  
  function getNextQuestion(st ){
	  var url = urls[ st ];
	  $.get( url + $("#questionIndex").val(), function( result ) {
		  var data = eval("(" + result + ")");
		  $("#questionIndex").val(data.questionIndex);
		  $(".answer-info").html(data.question);
	  } );
  }

  function submit ( _this ) {
    var $elems = $( _this ).find( 'input[type="text"]' );
    var isSubmit = false;

    $elems.each( function ( i, item ) {
      if ( item.isSubmit )
      {
        isSubmit = true;
      }
      else
      {
        isSubmit = false;
        return false;
      }
    } );

    return isSubmit;
  }

  function showErrorLayer ( _this ) {
    var $parent = $( _this ).parent();
    var $errorLayer = $parent.find( '.prompt-answer' );
    $errorLayer.show();
  }
  
  function hideErrorLayer ( _this ) {
    var $parent = $( _this ).parent();
    var $errorLayer = $parent.find( '.prompt-answer' );
    $errorLayer.hide();
  }

  return {
    init: init
  }
} );